var myw = screen.availWidth * 0.4;
var myh = screen.availHeight* 0.5;
$(function () {

    //begin列表信息字典目录
    $("#dataDictionary_datagrid").datagrid({
        title: "字典目录",
        url: "/dataDictionary1/list",
        columns: [[
            {field: "xxx", checkbox: 'true'},
            {field: "id", title: "编号", width: 100, hidden: true},
            {field: "sn", title: "字典目录编码", width: 100},
            {field: "name", title: "字典目录名称", width: 100},
            {field: "intro", title: "字典目录简介", width: 100}
        ]],
        width: 600,
        height: 600,
        pagination: true,
        fitColumns: true,
        rownumbers: true,
        singleSelect: true,
        striped: true,
        toolbar: "#dataDictionary_tb",
        onClickRow: function (index, row) {
            $("#dataDictionaryItem_datagrid").datagrid('load', {
                id: row.id
            })
        }
    });
    //end列表信息字典目录

    //begin列表信息字典明细
    $("#dataDictionaryItem_datagrid").datagrid({
        title: "字典目录明细",
        url: "/dataDictionaryItem/selectByParentId",
        columns: [[
            {field: "xxx", checkbox: 'true'},
            {field: "id", title: "编号", width: 100, hidden: true},
            {field: "name", title: "明细名称", width: 100},
            {field: "intro", title: "明细简介", width: 100},
            {
                field: "parent", title: "目录名称", width: 100, formatter: function (value, row, index) {
                console.log(value, row, index)
                if (value) {
                    return value.name;
                } else {
                    return "";
                }
            }
            }
        ]],
        width: 600,
        height: 600,
        fitColumns: true,
        rownumbers: true,
        singleSelect: true,
        striped: true,
        pageNumber:1,
        pageSize:20,
        pageList:[10,20,30,50],
        toolbar: "#dataDictionaryItem_tb"
    });
    //end列表信息字典明细


    //begin添加/编辑对话框字典目录
    $("#dataDictionary_dialog").dialog({
        width: myw,
        height: myh,
        maximizable:true,
        minimizable:true,
        collapsible:true,
        buttons: "#dataDictionary_bb",
        closed: true
    });
    //end添加/编辑对话框字典目录

    //begin添加/编辑对话框字典明细
    $("#dataDictionaryItem_dialog").dialog({
        width: myw,
        height: myh,
        maximizable:true,
        minimizable:true,
        collapsible:true,
        buttons: "#dataDictionaryItem_bb",
        closed: true
    });
    //end添加/编辑对话框字典明细

    //begin处理功能方法的对象
    var obj = {
        //begin添加按钮
        add: function () {
            $("#dataDictionary_form").form("clear");
            $("#dataDictionary_dialog").dialog("setTitle", "字典目录添加");
            $("#dataDictionary_dialog").dialog("open");
        },
        //end添加按钮

        //begin编辑按钮
        edit: function () {
            $("#dataDictionary_form").form("clear");
            $("#dataDictionary_dialog").dialog("setTitle", "字典目录编辑");
            var row = $("#dataDictionary_datagrid").datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择要操作的行!');
                return;
            }
            $("#dataDictionary_form").form("load", row);
            $("#dataDictionary_dialog").dialog("open");
        },
        //end编辑按钮

        //begin取消按钮
        cancel: function () {
            $("#dataDictionary_dialog").dialog("close");
        },
        //end取消按钮

        //begin保存按钮
        save: function () {
            var dataDictionaryId = $("#dataDictionaryId").val();
            var url;
            if (dataDictionaryId) {
                url = "/dataDictionary1/update"
            } else {
                url = "/dataDictionary1/save"
            }
            $("#dataDictionary_form").form("submit", {
                url: url,
                success: function (data) {
                    var data = $.parseJSON(data);
                    if (!data.success) {
                        $.messager.alert('温馨提示', data.msg);
                    } else {
                        $.messager.alert('温馨提示', "操作成功!");
                        $("#dataDictionary_dialog").dialog("close");
                        $("#dataDictionary_datagrid").datagrid("reload");
                    }
                }
            });
        },
        //end保存按钮

        //begin删除按钮
        remove: function () {
            var row = $("#dataDictionary_datagrid").datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择要操作的行!');
                return;
            }
            $.messager.confirm('确认', '您确认要操作此行吗？', function (r) {
                if (r) {
                    //注意这里的id要从当前选中的行中获取
                    var dataDictionaryId = row.id;
                    $.get("/dataDictionary1/delete", {dataDictionaryId: dataDictionaryId}, function (data) {
                        if (!data.success) {
                            $.messager.alert('温馨提示', data.msg);
                        } else {
                            $.messager.alert('温馨提示', '操作成功!');
                            $("#dataDictionary_datagrid").datagrid("reload");
                            $("#dataDictionaryItem_datagrid").datagrid("reload");
                        }
                    });
                }
            });

        },
        //end删除按钮

        //begin刷新按钮
        reload: function () {
            $("#dataDictionary_datagrid").datagrid("reload")
        },
        //end刷新按钮


        /*
         *
         * 字典明细
         * */


        //begin添加按钮
        iadd: function () {
            $("#dataDictionaryItem_form").form("clear");
            $("#dataDictionaryItem_dialog").dialog("setTitle", "字典明细添加");
            var row = $("#dataDictionary_datagrid").datagrid("getSelected");
            var item_row = $("#dataDictionaryItem_datagrid").datagrid("getSelected");
            if (row) {
                $("#parentDictionary").combobox("setValue", row.id);
                if (item_row) {
                    $("#parentDictionary").combobox("readonly", true);
                }
            }
            $("#dataDictionaryItem_dialog").dialog("open");
            $("#parentDictionary").combobox("readonly", true);
        },
        //end添加按钮

        //begin编辑按钮
        iedit: function () {
            $("#dataDictionaryItem_form").form("clear");
            $("#dataDictionaryItem_dialog").dialog("setTitle", "字典明细编辑");
            var row = $("#dataDictionaryItem_datagrid").datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择要操作的行!');
                return;
            }
            if (row.parent) {
                row["parent.id"] = row.parent.id;
            }
            $("#parentDictionary").combobox("readonly", true);
            $("#dataDictionaryItem_form").form("load", row);
            $("#dataDictionaryItem_dialog").dialog("open");
        },
        //end编辑按钮

        //begin取消按钮
        icancel: function () {
            $("#dataDictionaryItem_dialog").dialog("close");
        },
        //end取消按钮

        //begin保存按钮
        isave: function () {
            var dataDictionaryItemId = $("#dataDictionaryItemId").val();
            var url;
            if (dataDictionaryItemId) {
                url = "/dataDictionaryItem/update"
            } else {
                url = "/dataDictionaryItem/save"
            }
            $("#dataDictionaryItem_form").form("submit", {
                url: url,
                success: function (data) {
                    var data = $.parseJSON(data);
                    if (!data.success) {
                        $.messager.alert('温馨提示', data.msg);
                    } else {
                        $.messager.alert('温馨提示', "操作成功!");
                        $("#dataDictionaryItem_dialog").dialog("close");
                        $("#dataDictionaryItem_datagrid").datagrid("reload");
                    }
                }
            });
        },
        //end保存按钮

        //begin删除按钮
        iremove: function () {
            var row = $("#dataDictionaryItem_datagrid").datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择要操作的行!');
                return;
            }
            $.messager.confirm('确认', '您确认要操作此行吗？', function (r) {
                if (r) {
                    //注意这里的id要从当前选中的行中获取
                    var dataDictionaryItemId = row.id;
                    $.get("/dataDictionaryItem/delete", {dataDictionaryItemId: dataDictionaryItemId}, function (data) {
                        if (!data.success) {
                            $.messager.alert('温馨提示', data.msg);
                        } else {
                            $.messager.alert('温馨提示', '操作成功!');
                            $("#dataDictionaryItem_datagrid").datagrid("reload");
                        }
                    });
                }
            });

        },
        //end删除按钮

        //begin刷新按钮
        ireload: function () {
            $("#dataDictionaryItem_datagrid").datagrid("reload")
        }
        //end刷新按钮


    };
    //end处理功能方法的对象

    //begin处理功能方法对象,实际调用方法
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        obj[cmd]();
    });
    //end处理功能方法对象,实际调用方法

});